package com.hskn.hss.module.permission.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hskn.hss.core.annotation.SysLog;
import com.hskn.hss.core.enums.BusinessType;
import com.hskn.hss.entity.AjaxResult;
import com.hskn.hss.exception.HssException;
import com.hskn.hss.module.maintaincfg.mapper.TlMaintainCfgMapper;
import com.hskn.hss.module.outstorageCfg.entity.TlOutStorageCfg;
import com.hskn.hss.module.permission.entity.SysPermission;
import com.hskn.hss.module.permission.mapper.SysPermissionMapper;
import com.hskn.hss.module.permission.service.ISysPermissionService;
import com.hskn.hss.utils.Tools;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author hskn
 * @since 2021-04-30
 */
@Slf4j
@RestController
@RequestMapping("/hss/syspermission")
public class SysPermissionController {

    @Autowired
    ISysPermissionService sysPermissionService;

    @SysLog(businessType = BusinessType.INSERT, tableName = "sys_permission", bean = SysPermission.class, beanMapper = SysPermissionMapper.class, names = "id")
    @RequestMapping(value = "insertPermission", method = RequestMethod.POST)
    public AjaxResult<Object> insertPermission(HttpServletRequest request) {
        try {
            JSONObject params = JSON.parseObject(Tools.getBody(request));
            return sysPermissionService.insert(params);
        } catch (HssException e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败，请稍后再试");
        }
    }

    @SysLog(businessType = BusinessType.UPDATE, tableName = "sys_permission", idKey = "id", bean = SysPermission.class, beanMapper = SysPermissionMapper.class, names = "id")
    @RequestMapping(value = "updatePermission", method = RequestMethod.POST)
    public AjaxResult<Object> updatePermission(HttpServletRequest request) {
        try {
            JSONObject params = JSON.parseObject(Tools.getBody(request));
            return sysPermissionService.update(params);
        } catch (HssException e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败，请稍后再试");
        }
    }


    @RequestMapping(value = "getPermissionbyid", method = RequestMethod.POST)
    public AjaxResult<Object> getPermissionbyid(HttpServletRequest request) {
        JSONObject params = JSON.parseObject(Tools.getBody(request));
        SysPermission sysPermission = null;
        try {
            sysPermission = sysPermissionService.getSysPermissionById(params);
        } catch (HssException e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("获取信息失败，请稍后再试");
        }
        return AjaxResult.success(sysPermission);

    }

    @RequestMapping(value = "getPermissionlist", method = RequestMethod.POST)
    public AjaxResult<Object> getPermissionlist(HttpServletRequest request) {
        JSONObject params = JSON.parseObject(Tools.getBody(request));
        List<SysPermission> permissionList = null;
        try {
            permissionList = sysPermissionService.getSysPermissionList(params);
        } catch (HssException e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("获取信息失败，请稍后再试");
        }
        return AjaxResult.success(permissionList);
    }
}

